// 本文件实现CongestionMark组件
// @Author: zhengqi Wu
// @Description:
// @Version: 1.0.0
// @Date: 2021/3/9 21:13
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const encoding = require('../encoding')
var {InitialAble,TlvComponentBase} = require('./TlvComponentBase')
var aggregation = require('../common/aggregation')//实现了多继承

class CongestionMark extends aggregation(encoding.SelfEncodingBase,TlvComponentBase,InitialAble){
    constructor() {
        super();
        this.congestionMark = BigInt(0)
    }

    /**
     * 获取congestionMark
     * @returns {bigint}
     */
    getCongestionLevel(){
        return this.congestionMark
    }

    /**
     * 设置congestionMark
     * @param congestionMark bigint
     */
    setCongestionLevel(congestionMark)
    {
        this.congestionMark = BigInt(congestionMark)
        this.doInitial()
    }

    /**
     * @description 将 CongestionMark 线速编码为一个 TLV
     * @param enc {Encoder}
     * @returns totalLength {number}
     */
    wireEncode(enc){
        let totalLength = 0
        try{
            // 编码TLV-VALUE
            let tmpLen = enc.prependNonNegativeInteger(this.congestionMark)
            // DONE 错误处理
            totalLength += tmpLen

            // 编码TLV-LENGTH
            tmpLen = enc.prependVarNumber((totalLength))
            // DONE 错误处理
            totalLength += tmpLen

            // 编码 TLV-TYPE
            tmpLen = enc.prependVarNumber((encoding.tlv.ComponentType.TlvCongestionMark))
            // DONE 错误处理
            totalLength += tmpLen

            return totalLength
        }catch (e){
            throw e
        }

    }

    /**
     * @description 从 TLV Block 中解码一个 CongestionMark
     * @param block {encoding.block.Block}
     */
    wireDecode(block){
        // 检查 Type 是否正确
        // getType => vlint
        try{
            if(encoding.tlv.expectType(block.getType(), (encoding.tlv.ComponentType.TlvCongestionMark))){
                // 读取 interestLifeTime
                // getValue => vlint
                let value = encoding.tlv.readNonNegativeInteger(block.getValue(), 0, block.getLength())
                this.setCongestionLevel(BigInt(value))
            }
        }catch (e){
            throw e
        }

    }
}

module.exports = {
    CongestionMark:CongestionMark
}